package cn.parllay.dingdangproject.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.parllay.dingdangproject.R;
import cn.parllay.dingdangproject.adapter.VipFragmentAdapter;
import cn.parllay.dingdangproject.bean.customer.VIPCustomerResult;
import cn.parllay.dingdangproject.bean.customer.VipCustomer;
import cn.parllay.dingdangproject.global.API;
import cn.parllay.dingdangproject.global.QueryString;
import cn.parllay.dingdangproject.utils.NetWorkUtils;
import cn.parllay.dingdangproject.utils.SpUtils;
import cn.parllay.dingdangproject.utils.ToastUtils;

public class VipCustomerActivity extends BaseActivity implements OnRefreshListener, OnLoadmoreListener {

    @BindView(R.id.recyclerview_viplist)
    RecyclerView mRecyclerview;

    @BindView(R.id.tv_search)
    TextView tv_search;

    @BindView(R.id.vip_refreshLayout)
    SmartRefreshLayout vip_refreshLayout;

    private static final String TAG = "VipCustomerActivity";
    private VipFragmentAdapter mAdapter = null;
    private int page = 1;
    private int size = 20;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_vip_customer);

        ButterKnife.bind(this);

        initView();

        initListener();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(vipListener != null) {
            vipListener = null;
        }
        if(onItemClickListener != null) {
            onItemClickListener = null;
        }

    }

    @OnClick(R.id.tv_search)
    void onSearchClick(){
        ToastUtils.showToast("查询");
    }

    private void initListener() {
        vip_refreshLayout.setOnRefreshListener(this);
        vip_refreshLayout.setOnLoadmoreListener(this);
        mAdapter.setmOnItemClickListener(onItemClickListener);
    }

    private void initView() {

        mRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new VipFragmentAdapter(this);
        mRecyclerview.setAdapter(mAdapter);

        vip_refreshLayout.setEnableAutoLoadmore(true);
        vip_refreshLayout.setEnableRefresh(true);
    }


    private VipFragmentAdapter.OnItemClickListener onItemClickListener = new VipFragmentAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View view, String contactId) {
            toCustomerInfoActivity(contactId);
        }
    };


    //点击跳转到顾客详情页
    private void toCustomerInfoActivity(String contactId) {
        CustomerInfoActivity.actionStart(this, contactId);
    }

    @Override
    public void onResume() {
        super.onResume();
        //从服务端获取VIP客户列表

        page = 1;
        getVipListFromServer();
    }

    private void getVipListFromServer() {
        HashMap<String, String> params = new HashMap<>();
        params.put(QueryString.PAGE, String.valueOf(page));
        params.put(QueryString.SIZE, String.valueOf(size));
        int storeId = SpUtils.getInstance(this).getInt(SpUtils.STORE_ID, 0);
        params.put(QueryString.STORE_ID, String.valueOf(storeId));
        NetWorkUtils.doGET(API.listVip(), params, VIPCustomerResult.class, vipListener);
    }

    private NetWorkUtils.OnRequestListener vipListener = new NetWorkUtils.OnRequestListener<VIPCustomerResult>() {
        @Override
        public void onSuccess(VIPCustomerResult vipCustomerResult) {

                List<VipCustomer> vipCustomerList = vipCustomerResult.getData();
                if(vipCustomerList == null) {
                    return;
                }

                if (page == 1) {
                    mAdapter.refresh(vipCustomerList);
                    vip_refreshLayout.finishRefresh();
                    vip_refreshLayout.setLoadmoreFinished(false);
                    if (vipCustomerList != null && page * size >= vipCustomerResult.getTotal()) {
                        vip_refreshLayout.setLoadmoreFinished(true);
                    }
                } else {
                    mAdapter.loadMore(vipCustomerList);
                    vip_refreshLayout.finishLoadmore();
                    if (page * size >= vipCustomerResult.getTotal()) {
                        vip_refreshLayout.setLoadmoreFinished(true);
                    }
                }

        }

        @Override
        public void onError(String errorMsg) {

        }
    };

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        page = 1;
        getVipListFromServer();
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        page++;
        getVipListFromServer();
    }
}
